/**
 * 用户-数据库操作模块
 */
const DbPool = require('./dbpool');

module.exports = class User_module extends DbPool {
  /**
   * 获取用户数据,登录
   * @param {string} username
   * @param {string} password
   * @param {number} type
   * @return {Promise <any>}
   */
  static getUser(username, password, type) {
    type = Number(type);
    return new Promise((resolve, reject) => {
      const sql = `select * from userinfo where username=? and password=? and type=?`;
      this.query(sql, this.formatParams(username, password, type)).then((result) => {
        resolve(result);
      }).catch((error) => {
        console.log('getUser error in user_module');
        reject(error);
      })
    })
  }

  /**
   * 通过用户 id 获取用户数据
   * @param {string} uid
   * @return {Promise <any>}
   */
  static getUserDataById(uid) {
    return new Promise((resolve, reject) => {
      const sql = `select * from userinfo where id=?`;
      this.query(sql, this.formatParams(uid)).then((results) => {
        resolve(results);
      }).catch((error) => {
        console.log('getUserById error in users_module');
        reject(error);
      })
    })
  }

  /**
   * 更新头像
   * @param {string} newAvatar  头像文件名称，暂从本地固定
   * @param {number} id
   * @return {Promise <any>} 
   */
  static upAvatar(newAvatar, id) {
    return new Promise((resolve, reject) => {
      const sql = `update userinfo set head=? where id=?`;
      this.query(sql, this.formatParams(newAvatar, id)).then((results) => {
        resolve(results);
      }).catch((error) => {
        console.log('upAvatar error in users_module');
        reject(error);
      })
    })
  }

  /**
   * 更新密码
   * @param {string} oldpwd 旧密码
   * @param {string} newpwd  新密码
   * @param {number} id   用户id
   * @returns {Promise <any>}
   */
  static upPassword(oldpwd, newpwd, id) {
    return new Promise((resolve, reject) => {
      const sql = `update userinfo set password=? where id=? and password=?`;
      this.query(sql, this.formatParams(newpwd, id, oldpwd)).then((result) => {
        resolve(result);
      }).catch((error) => {
        reject(error);
      })
    })
  }

  /**
   * 获取用户列表
   * @param type 用户类型
   * @returns {Promise <any>}
   */
  static getlistByType(type) {
    type = Number(type);
    return new Promise((resolve, reject) => {
      const sql = `select id,username,address,classes from userinfo where type=?`;
      this.query(sql, this.formatParams(type)).then((result) => {
        resolve(result);
      }).catch((error) => {
        reject(error);
      })
    })
  }

  /**
   * 根据用户类型获取用户列表（分页）
   * @param {number} type  用户类型
   * @param {number} curPage   当前页
   * @param {number} pageNum    当前页的数据数量
   * @return {Promise <any>}
   */
  static getlistByTypePage(type, curPage, pageNum) {
    type = Number(type);
    curPage = Number(curPage);
    pageNum = Number(pageNum);
    return new Promise((resolve, reject) => {
      const sql = `select id,username,address,classes from userinfo where type=? order by modifytime desc limit ?,?`;
      this.query(sql, this.formatParams(type, curPage, pageNum)).then((result) => {
        resolve(result);
      }).catch((error) => {
        reject(error);
      })
    })
  }

  /**
 * 获取通知（分页）
 * @param {number} curPage
 * @param {number} pageNum
 * @return {Promise <any>}
 */
  static getNotices(curPage, pageNum) {
    curPage = Number(curPage);
    pageNum = Number(pageNum);
    return new Promise((resolve, reject) => {
      const sql = `select * from notice order by createtime desc limit ?,?`;
      this.query(sql, this.formatParams(curPage, pageNum)).then((result) => {
        resolve(result);
      }).catch((error) => {
        reject(error);
      })
    })
  }
}